package cn.edu.buaa.nlsde.grid.DatabaseOperate;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.edu.buaa.nlsde.grid.DatabaseConnect.DBConn;
import cn.edu.buaa.nlsde.grid.DatabaseConnect.DBInitial;

public class DataSimulator
{
	private static String sql;
	private static DBConn dbconn=new DBConn(DBInitial.getDriver(),DBInitial.getURL());
	private static Statement state;
	
	public int myRandom(int m,int n)
	{
		if(m>n)
		{
			return 0;
		}
        int reVal=(int)(java.lang.Math.random()*(n-m+1))+m;  
        
        return reVal;  
}
	
	public static void CpuSimulator(int HostID,String collectTime)
	{
		float cpu_nice=0;
		float cpu_wio=0;
		float cpu_aidle=(float)99.6;
		float cpu_idle=(float)99.5;
		float cpu_user=(float)0.4;
		float cpu_system=(float)0.1;
		sql="insert into Metric_CPU (HostID,cpu_nice,cpu_wio,cpu_aidle,cpu_idle,cpu_user,cpu_system,CollectTime) values ("+HostID+","+cpu_nice+","+cpu_wio+","+cpu_aidle+","+cpu_idle+","+cpu_user+","+cpu_system+",'"+collectTime+"')";
		try
		{
			state.executeUpdate(sql);
		}
		catch(SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void DiskSimulator(int HostID,String collectTime)
	{
		float disk_total=(float)156.499;
		float disk_free=(float)139.693;
		sql="insert into Metric_Disk (HostID,disk_total,disk_free,CollectTime) values ("+HostID+","+disk_total+","+disk_free+",'"+collectTime+"')";
		try
		{
			state.executeUpdate(sql);
		}
		catch(SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void LoadSimulator(int HostID,String collectTime)
	{
		float load_one=(float)0.08;
		float load_five=(float)0.06;
		float load_fifteen=(float)0.02;
		sql="insert into Metric_Load (HostID,load_one,load_five,load_fifteen,CollectTime) values ("+HostID+","+load_one+","+load_five+","+load_fifteen+",'"+collectTime+"')";
		try
		{
			state.executeUpdate(sql);
		}
		catch(SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void MemorySimulator(int HostID,String collectTime)
	{
		float swap_total=1020116;
		float swap_free=1019952;
		float mem_total=1556748;
		float mem_free=26456;
		float mem_cached=966284;
		float mem_buffers=80996;
		float mem_shared=0;
		sql="insert into Metric_Memory (HostID,swap_total,swap_free,mem_total,mem_free,mem_cached,mem_buffers,mem_shared,CollectTime) values ("+HostID+","+swap_total+","+swap_free+","+mem_total+","+mem_free+","+mem_cached+","+mem_buffers+","+mem_shared+",'"+collectTime+"')";
		try
		{
			state.executeUpdate(sql);
		}
		catch(SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void PktsSimulator(int HostID,String collectTime)
	{
		float bytes_in=(float) 244.37;
		float bytes_out=(float) 856.37;
		float pkts_in=(float) 3.18;
		float pkts_out=(float) 1.35;
		sql="insert into Metric_PKTS (HostID,bytes_in,bytes_out,pkts_in,pkts_out,CollectTime) values ("+HostID+","+bytes_in+","+bytes_out+","+pkts_in+","+pkts_out+",'"+collectTime+"')";
		try
		{
			state.executeUpdate(sql);
		}
		catch(SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void ProcessSimulator(int HostID,String collectTime)
	{
		float proc_total=182;
		float proc_run=0;
		sql="insert into Metric_Process (HostID,proc_total,proc_run,CollectTime) values ("+HostID+","+proc_total+","+proc_run+",'"+collectTime+"')";
		try
		{
			state.executeUpdate(sql);
		}
		catch(SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public static void main(String [] arg) throws SQLException
	{
		state=dbconn.connect();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long interval=30*1000;
		long datetime=System.currentTimeMillis()-60*60*1000;
		Date date=new Date(datetime);
		String collectTime=sdf.format(date);
		final int MAX=20;
		
		for(int i=1;i<MAX;i++)
		{
			datetime+=interval*i;
			collectTime=sdf.format(datetime);
			
			CpuSimulator(0,collectTime);
			DiskSimulator(0,collectTime);
			LoadSimulator(0,collectTime);
			MemorySimulator(0,collectTime);
			PktsSimulator(0,collectTime);
			ProcessSimulator(0,collectTime);
			
			CpuSimulator(1,collectTime);
			DiskSimulator(1,collectTime);
			LoadSimulator(1,collectTime);
			MemorySimulator(1,collectTime);
			PktsSimulator(1,collectTime);
			ProcessSimulator(1,collectTime);
			
			CpuSimulator(10,collectTime);
			DiskSimulator(10,collectTime);
			LoadSimulator(10,collectTime);
			MemorySimulator(10,collectTime);
			PktsSimulator(10,collectTime);
			ProcessSimulator(10,collectTime);
		}
	}
}
